package com.sc.sysconfig.controller;

import com.sc.sysconfig.dto.MenuQueryDTO;
import com.sc.sysconfig.entity.SysMenu;
import com.sc.sysconfig.entity.SysUser;
import com.sc.sysconfig.service.SysMenuService;
import com.sc.sysconfig.vo.CascaderVO;
import com.sc.sysconfig.vo.OptionVO;
import com.sc.sysconfig.vo.PageVO;
import com.sc.sysconfig.vo.TreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 系统菜单控制器
 *
 * @author 徐赫阳
 * @date 2021/09/13
 */
@Api(description = "菜单")
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    /**
     * 服务对象
     */
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("分页获取菜单")
    @PostMapping("/getMenuList")
    public PageVO<SysMenu> getMenuList(@RequestBody MenuQueryDTO queryDTO){
        return sysMenuService.getMenuList(queryDTO);
    }

    @ApiOperation("分页获取菜单按钮")
    @PostMapping("/getMenuButtonList")
    public PageVO<SysMenu> getMenuButtonList(@RequestBody MenuQueryDTO queryDTO){
        return sysMenuService.getMenuButtonList(queryDTO);
    }

    @ApiOperation("根据id获取菜单")
    @GetMapping("/getMenuById/{id}")
    public SysMenu getMenuById(@PathVariable Integer id){
        return sysMenuService.getById(id);
    }

    @ApiOperation("保存菜单")
    @PostMapping("/saveMenu")
    public Integer saveMenu(@RequestBody SysMenu sysMenu, HttpServletRequest request) throws Exception{
        if(sysMenu.getParent() != null && sysMenu.getId() != null && sysMenu.getParent() == sysMenu.getId()){
            throw new Exception("不可以嵌套选择哦！~");
        }
        if(sysMenu.getId() == null){
            sysMenu.setCreatorId((Integer)request.getSession(true).getAttribute("user"));
        }else{
            sysMenu.setUpdaterId((Integer)request.getSession(true).getAttribute("user"));
        }
        sysMenuService.saveOrUpdate(sysMenu);
        return sysMenu.getId();
    }

    @ApiOperation("获取菜单下拉列表")
    @GetMapping(value = "/getMenuOption")
    public List<OptionVO> getMenuOption(){
        return sysMenuService.getMenuOption();
    }

    @ApiOperation("获取菜单下拉列表(含子菜单)")
    @GetMapping(value = "/getMenuCascader")
    public List<CascaderVO> getMenuCascader(){
        return sysMenuService.getMenuCascader();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/deleteMenu/{id}")
    public void deleteMenu(@PathVariable Integer id){
        sysMenuService.removeById(id);
    }

    @ApiOperation("获取用户菜单")
    @PostMapping(value = "/getUserMenu")
    public List<SysMenu> getUserMenu(@RequestBody SysUser user){
        return sysMenuService.getUserMenu(user.getLoginName());
    }

    @ApiOperation("获取菜单树")
    @GetMapping(value = "/getMenuTree")
    public List<TreeVO> getMenuTree(){
        return sysMenuService.getMenuTree();
    }

}
